﻿using Entity.BaseEntitys;
using Entity.BaseSplit;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Sharding.PaginationConfigurations;
using ShardingCore.VirtualRoutes.Mods;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Entity.BaseSplitRoute
{
    public class BaseSplitModTableRoute<T, Key> : AbstractSimpleShardingModKeyStringVirtualTableRoute<T> where T : Entity<Key>
    {
        private static readonly int sourceCount = SplitAreaSource.GetAllSource().Length;

        public BaseSplitModTableRoute() : base(sourceCount, sourceCount, '0')
        {
        }

        public override void Configure(EntityMetadataTableBuilder<T> builder)
         => builder.ShardingProperty(o => o.SplitArea);

        public override bool? EnableRouteParseCompileCache => true;


        public override IPaginationConfiguration<T> CreatePaginationConfiguration()
            => new SplitModTablePaginationConfiguration<T, Key>();
    }

    public class SplitModTablePaginationConfiguration<T, Key> : IPaginationConfiguration<T> where T : Entity<Key>
    {
        public void Configure(PaginationBuilder<T> builder)
        {
            builder.PaginationSequence(o => o.Id)
                .UseRouteComparer(Comparer<string>.Default)
                .UseQueryMatch(PaginationMatchEnum.Owner | PaginationMatchEnum.Named | PaginationMatchEnum.PrimaryMatch).UseAppendIfOrderNone(10);

            builder.ConfigReverseShardingPage(1d, 10000L);
        }
    }
}
